package com.sourcetrip.raw.adt.abs;


import com.sourcetrip.raw.adt.interfaces.MyLinkedListInterfaces;

/**
 * @author: ZhouBert
 * @date: 2020/11/18
 * @description: 抽出 myLinkedList 共性方法的抽象类
 */
public abstract class MyAbstractLinkedList<E> implements MyLinkedListInterfaces<E> {

	protected int size;

	@Override
	public int size() {
		return size;
	}

	@Override
	public boolean isEmpty() {
		return size == 0;
	}

	@Override
	public void add(E element) {
		add(size, element);
	}


	@Override
	public boolean contains(E element) {
		return indexOf(element) != -1;
	}

	/**
	 * get 时检查索引
	 *
	 * @param index
	 */
	protected void checkIndex4get(int index) {
		if (index < 0 || index >= size) {
			throw new IndexOutOfBoundsException("索引越界！index: "+index);
		}
	}

	/**
	 * add 时检查索引
	 *
	 * @param index
	 */
	protected void checkIndex4add(int index) {
		if (index < 0 || index > size) {
			throw new IndexOutOfBoundsException("索引越界！index: "+index);
		}
	}
}
